const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')
const buyFlag = require('./buy')
const sellFlag = require('./sell')
const pingKongFlag = require('./pingKong')
const pingDuoFlag = require('./pingDuo')
const noSellFlag = require('./noSell')
const noBuyFlag = require('./noBuy')
const commonFlag = require('./common')
const tendencyDuoFlag = require('./tendencyDuo')
const tendencyKongFlag = require('./tendencyKong')
const itemInfoUtil = require('../itemInfo')
const indexFlagUtil = require('../common/flag/indexFlag')

function getMinSg (btbItem) {
  const hourEmaInfo = btbItem.hourEmaInfo || {}
  const dayEmaInfo = btbItem.dayEmaInfo || {}
  const hour4EmaInfo = btbItem.hour4EmaInfo || {}
  let sg = hourEmaInfo.sg
  if (hour4EmaInfo.sg < sg) {
    sg = hour4EmaInfo.sg
  }
  if (dayEmaInfo.sg < sg) {
    sg = dayEmaInfo.sg
  }
  return sg
}

function BuySellPolicy (config) {
  const nowDate = new Date()
  this.hour = nowDate.getHours()
  this.minute = nowDate.getMinutes()
  this.isStartOfHour = this.minute < 16
  this.isStartOfHour4 = [0, 4, 8, 12, 16, 20].indexOf(this.hour) !== -1
  this.buy = false
  this.buyReason = ''
  this.sell = false
  this.sellReason = ''
  this.pingKong = false
  this.pingKongReason = ''
  this.pingDuo = false
  this.pingDuoReason = ''
  this.buyRate = 1
  this.sellRate = 1
  this.buyRateForce = 0
  this.sellRateForce = 0
  // 满仓
  this.fullPosition = false
  // 禁用合约
  this.disabled = config.btbItem.hy_status === 0
  this.btbItem = config.btbItem
  this.btbOpInfoJson = config.btbOpInfoJson
  this.opConfig = config.opConfig
  this.btbIndexTotalInfo = config.btbIndexTotalInfo
  this.sysConfig = config.sysConfig || {}
  this.close = this.btbItem.close

  const btbOpInfo = this.btbOpInfoJson[this.btbItem.code] || {}
  btbOpInfo.actionStatus = btbOpInfo.actionStatus || ''
  btbOpInfo.highWaveStatus = btbOpInfo.highWaveStatus || ''

  this.btbOpInfo = btbOpInfo
  this.emInfo = this.btbItem.asInfo[0] || {}
  this.initEmInfo()
  this.indexFlagInfo = indexFlagUtil.getAllFlag(this.btbItem)
  this.initPositionInfo()
}

BuySellPolicy.prototype = {
  initPositionInfo () {
    // 计算持仓盈利
    let kongProfitHigh = false
    let duoProfitHigh = false
    if (this.btbItem.incomeRate > 10) {
      if (this.btbItem.usdt < -5) {
        kongProfitHigh = true
      }
      if (this.btbItem.usdt > 5) {
        duoProfitHigh = true
      }
    }
    this.hasKongPosition = this.btbItem.usdt < -1
    this.hasDuoPosition = this.btbItem.usdt > 1
    this.kongProfitHigh = kongProfitHigh
    this.duoProfitHigh = duoProfitHigh
  },
  initEmInfo () {
    const btbItem = this.btbItem
    btbItem.monthEmaInfo = itemInfoUtil.formatEmaInfo(btbItem.monthEmaInfo || {}, 'monthEmaInfo')
    btbItem.weekEmaInfo = itemInfoUtil.formatEmaInfo(btbItem.weekEmaInfo || {}, 'weekEmaInfo')
    btbItem.dayEmaInfo = itemInfoUtil.formatEmaInfo(btbItem.dayEmaInfo || {}, 'dayEmaInfo')
    btbItem.hour4EmaInfo = itemInfoUtil.formatEmaInfo(btbItem.hour4EmaInfo || {}, 'hour4EmaInfo')
    btbItem.hourEmaInfo = itemInfoUtil.formatEmaInfo(btbItem.hourEmaInfo || {}, 'hourEmaInfo')
    this.emInfo.kdjDiff = this.emInfo.kdj ? (this.emInfo.kdj.J - this.emInfo.kdj.D) : 0
    btbItem.minute15EmaInfo = itemInfoUtil.formatEmaInfo(btbItem.minute15EmaInfo || {}, 'minute15EmaInfo')
    btbItem.minSg = getMinSg(btbItem)
    itemInfoUtil.resetEmaInfo(btbItem, {
      isStartOfHour: this.isStartOfHour,
      isStartOfHour4: this.isStartOfHour4
    })
  },
  setSell (text) {
    this.sell = true
    this.pingDuo = false
    this.buy = false
    this.pingKong = false
    this.buyRate = 1
    this.sellRate = 1
    this.sellReason = text || ''
    this.pingKongReason = ''
    this.buyReason = ''
    this.pingDuoReason = ''
  },
  setBuy (text) {
    this.buy = true
    this.pingKong = false
    this.sell = false
    this.pingDuo = false
    this.buyRate = 1
    this.sellRate = 1
    this.buyReason = text || ''
    this.pingKongReason = ''
    this.pingDuoReason = ''
    this.sellReason = ''
  },
  setPingKong (text) {
    this.pingKong = true
    this.pingKongReason = text || ''
    this.sell = false
    this.sellReason = ''
  },
  setPingDuo (text) {
    this.pingDuo = true
    this.pingDuoReason = text || ''
    this.buy = false
    this.buyReason = ''
  },
  setNoBuy () {
    this.buy = false
    this.buyReason = ''
  },
  setNoSell () {
    this.sell = false
    this.sellReason = ''
  },
  setNoPingDuo () {
    this.pingDuo = false
    this.pingDuoReason = ''
  },
  setNoPingKong () {
    this.pingKong = false
    this.pingKongReason = ''
  },
  ...pingKongFlag,
  ...sellFlag,
  ...pingDuoFlag,
  ...buyFlag,
  ...noSellFlag,
  ...noBuyFlag,
  ...commonFlag,
  ...tendencyDuoFlag,
  ...tendencyKongFlag,
  getResult () {
    this.exV2()
  },
  exV1 () {
    const {
      isNewIndex
    } = this.indexFlagInfo
    const weekEmaInfo = this.btbItem.weekEmaInfo || {}
    if (isNewIndex) {
      this.newIndexBuySell()
    } else if (this.btbItem.tendency === 'duo') {
      this.tendencyDuoBuySellNormal()
    } else if (this.btbItem.tendency === 'kong') {
      this.tendencyKongBuySellNormal()
    } else {
      if (weekEmaInfo.isKdjDecline && weekEmaInfo.isMacdDecline) {
        this.weekKongBuySellNormal()
      } else if (weekEmaInfo.isKdjIncrease && weekEmaInfo.isMacdIncrease) {
        this.weekDuoBuySellNormal()
      } else {
        this.newIndexBuySell('noTendencyBuySell')
      }
    }
    this.noBuyIfNewUp()
    this.noSellIfNewDown()
    this.noBuySellIfWaveLow()
    // 币的sg越大，行情越确定，大行情的日sg都很大
    // 后面看看要不要给更大仓位
    this.buyIfEmaLongSgHigh()
    this.sellIfEmaLongSgHigh()
    this.pingDuoIfStopLoss()
    this.pingDuoIfTargetProfit()
    if (this.sell || this.pingDuo) {
      this.btbOpInfo.actionStatus = ''
    }
    if (!this.hasDuoPosition && this.pingDuo) {
      this.pingDuo = false
      this.pingDuoReason = ''
    }
    if (!this.hasKongPosition && this.pingKong) {
      this.pingKong = false
      this.pingKongReason = ''
    }
  },
  exV2 () {
    const {
      isNewIndex
    } = this.indexFlagInfo
    const weekEmaInfo = this.btbItem.weekEmaInfo || {}
    if (isNewIndex) {
      this.newIndexBuySell()
    } else if (this.btbItem.tendency === 'duo') {
      this.tendencyDuoBuySellNormal()
    } else if (this.btbItem.tendency === 'kong') {
      this.tendencyKongBuySellNormal()
    } else {
      if (weekEmaInfo.isKdjDecline && weekEmaInfo.isMacdDecline) {
        this.weekKongBuySellNormal()
      } else if (weekEmaInfo.isKdjIncrease && weekEmaInfo.isMacdIncrease) {
        this.weekDuoBuySellNormal()
      } else {
        this.newIndexBuySell('noTendencyBuySell')
      }
    }
    this.noBuyIfNewUp()
    this.noSellIfNewDown()
    this.noBuySellIfWaveLow()
    this.noBuySellIfNoLong()
    // 币的sg越大，行情越确定，大行情的日sg都很大
    // 后面看看要不要给更大仓位
    this.buyIfEmaLongSgHigh()
    this.sellIfEmaLongSgHigh()
    this.pingDuoIfStopLoss()
    this.pingDuoIfTargetProfit()
    if (this.sell || this.pingDuo) {
      this.btbOpInfo.actionStatus = ''
    }
    if (!this.hasDuoPosition && this.pingDuo) {
      this.pingDuo = false
      this.pingDuoReason = ''
    }
    if (!this.hasKongPosition && this.pingKong) {
      this.pingKong = false
      this.pingKongReason = ''
    }
  },
  getPositionSuggest () {
    let resultPosition = this.opConfig.btbPositionConfig
    let proportion = this.btbItem.proportion || 1
    if (this.sysConfig.btbPositionConfig === '自动') {
      if (btbAs.canUseMaxPosition({
        btbItem: this.btbItem
      })) {
        resultPosition = 1
      }
      // if (this.canUseMaxPosition({
      //   dayEmaud: 15,
      //   hour4Emaud: 15
      // })) {
      // }
      // 10个指数，但真正交易的可能就5个
      proportion = 1 + btbAs.countProportionDiff({
        btbItem: this.btbItem
      })
    }
    return {
      resultPosition,
      proportion
    }
  }
}

module.exports = BuySellPolicy
